package com.valley.dream.tihai.presenter.impl;

import android.app.Activity;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.valley.dream.tihai.MainActivity;
import com.valley.dream.tihai.R;
import com.valley.dream.tihai.model.entity.Type;
import com.valley.dream.tihai.presenter.ITypePresenter;
import com.valley.dream.tihai.service.TypeService;
import com.valley.dream.tihai.util.network.RetrofitUtil;
import com.valley.dream.tihai.util.result.Result;
import com.valley.dream.tihai.view.adapter.CommunityRVAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TypePresenterImpl implements ITypePresenter {

    TypeService typeService = RetrofitUtil.getTypeService();

    @Override
    public Result selectAllType(Activity activity) {
        Call<List<Type>> listCall = typeService.selectAllType();
        RecyclerView recyclerView = activity.findViewById(R.id.community_recyclerview);
        listCall.enqueue(new Callback<List<Type>>() {
            @Override
            public void onResponse(Call<List<Type>> call, Response<List<Type>> response) {
                List<Type> body = response.body();
                CommunityRVAdapter adapter = new CommunityRVAdapter(activity, body);
                adapter.setListener(new CommunityRVAdapter.MyListener() {
                    @Override
                    public void onClick(int position) {
                        int id = body.get(position).getId();
                        MainActivity mainActivity = (MainActivity) activity;
                        mainActivity.enterCommunity(id);
                    }
                });
                recyclerView.setAdapter(adapter);
                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
            }

            @Override
            public void onFailure(Call<List<Type>> call, Throwable t) {

            }
        });
        return Result.ok();
    }

    @Override
    public Result selectTypeById(Activity activity, Integer typeId) {
        Call<Type> typeCall = typeService.selectTypeById(typeId);
        typeCall.enqueue(new Callback<Type>() {
            @Override
            public void onResponse(Call<Type> call, Response<Type> response) {

            }

            @Override
            public void onFailure(Call<Type> call, Throwable t) {

            }
        });
        return Result.ok();
    }
}
